/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tencent.tendon.convert.json;

/**
 *
 * @author nbzhang
 */
public abstract class JsonHandle<T, V> implements Comparable<JsonHandle<T, V>>, JsonSimpleable {

	public final String field;

	public final char[] fields;

	protected JsonAttribute<T, V> attribute;

	private int index; //顺序, 从0开始 0、1、2、3、4

	public JsonHandle(final JsonAttribute<T, V> attribute) {
		this.field = attribute == null ? "" : attribute.field();
		this.fields = field.toCharArray();
		this.attribute = attribute;
	}

	/**
	 * 单向链表数据结构
	 *
	 * @return
	 */
	public abstract JsonHandle<T, ?> next();

	public final int size() {
		JsonHandle handle = this;
		int size = 0;
		do {
			size++;
		} while ((handle = handle.next()) != null);
		return size;
	}

	@Override
	public final int compareTo(JsonHandle<T, V> o) {
		if (o == null) return 1;
		return this.field.compareTo(o.field);
	}

	final void index(int index) {
		this.index = index;
	}

	public final int index() {
		return index;
	}
}
